package hos.ui.item;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import java.util.Map;

import hos.ui.ItemViewUtils;
import hos.ui.R;

/**
 * <p>Title: ItemTextLayout </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/6/9 17:45
 */
public class ItemImageLayout extends ItemTitleLayout {

    private ImageView imageView;

    public ItemImageLayout(Context context) {
        this(context, null);
    }

    public ItemImageLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private static final ImageView.ScaleType[] sScaleTypeArray = {
            ImageView.ScaleType.MATRIX,
            ImageView.ScaleType.FIT_XY,
            ImageView.ScaleType.FIT_START,
            ImageView.ScaleType.FIT_CENTER,
            ImageView.ScaleType.FIT_END,
            ImageView.ScaleType.CENTER,
            ImageView.ScaleType.CENTER_CROP,
            ImageView.ScaleType.CENTER_INSIDE
    };

    public ItemImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void initAttributeSet(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.ItemImageLayout);
        try {
            imageView = new ImageView(context, attrs, defStyleAttr);
            final Drawable d = typedArray.getDrawable(R.styleable.ItemImageLayout_android_src);
            if (d != null) {
                imageView.setImageDrawable(d);
            }

            final int index = typedArray.getInt(R.styleable.ItemImageLayout_android_scaleType, -1);
            if (index >= 0) {
                imageView.setScaleType(sScaleTypeArray[index]);
            }

            if (typedArray.hasValue(R.styleable.ItemImageLayout_imageTint)) {
                ColorStateList drawableTintList = typedArray.getColorStateList(R.styleable.ItemImageLayout_imageTint);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    imageView.setImageTintList(drawableTintList);
                }
            }
            int imageWidth = ItemViewUtils.getLayoutParam(typedArray,R.styleable.ItemImageLayout_imageWidth, 0);
            int imageHeight = ItemViewUtils.getLayoutParam(typedArray,R.styleable.ItemImageLayout_imageHeight, 0);
            int orientation = getOrientation();
            if (orientation == HORIZONTAL) {
                int childGravity = typedArray.getInt(R.styleable.ItemImageLayout_imageGravity, 0);
                if (childGravity == 1) {
                    // 显示在右侧
                    View view = new View(context);
                    LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
                    layoutParams.weight = 1;
                    view.setLayoutParams(layoutParams);
                    addView(view);
                }
            }
            imageView.setLayoutParams(new LayoutParams(imageWidth, imageHeight));
            addView(imageView);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void setContent(Map<String, Object> map, String key) {
        setKey(key);
        if (imageView == null) {
            return;
        }
        if (!map.containsKey(key)) {
            return;
        }
        Object value = map.get(key);
        if (value == null) {
            imageView.setImageDrawable(null);
            return;
        }
        if (value instanceof Integer) {
            imageView.setImageResource((Integer) value);
            return;
        }
        if (value instanceof Drawable) {
            imageView.setImageDrawable((Drawable) value);
            return;
        }
        if (value instanceof Uri) {
            imageView.setImageURI((Uri) value);
        }
    }

    public void setImageDrawable(Drawable drawable) {
        if (imageView == null) {
            return;
        }
        imageView.setImageDrawable(drawable);
    }

    public void setImageURI(Uri uri) {
        if (imageView == null) {
            return;
        }
        imageView.setImageURI(uri);
    }

    public void setImageResource(int resId) {
        if (imageView == null) {
            return;
        }
        imageView.setImageResource(resId);
    }

    @Override
    public void setContent(CharSequence content) {

    }

    @Override
    public void clear() {
        if (imageView == null) {
            return;
        }
        imageView.setImageDrawable(null);
    }


    @Override
    public void clearListener() {
        super.clearListener();
        if (imageView != null) {
            imageView.setOnClickListener(null);
        }
    }


    public ImageView getImageView() {
        return imageView;
    }

    public void setOnContentClickListener(OnClickListener l) {
        if (imageView == null) {
            return;
        }
        imageView.setOnClickListener(l);
    }

    public void setContentEnabled(boolean enabled) {
        if (imageView != null) {
            imageView.setEnabled(enabled);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (imageView != null) {
            imageView.setEnabled(enabled);
        }
        super.setEnabled(enabled);
    }

    public void setClickableEnabled(boolean clickable) {
        if (imageView != null) {
            imageView.setClickable(clickable);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        if (imageView != null) {
            imageView.setClickable(clickable);
        }
        super.setClickable(clickable);
    }
}
